<?php

/**
 * 
 * @author Youssef Driouach
 *
 */
class Application_Model_LocTree
{
	protected $_locTreeId;
	protected $_parentId;
	protected $_address;
	protected $_code;

	public function __construct(array $options = null)
	{
		if (is_array($options)) {
			$this->setOptions($options);
		}
	}

	public function __set($name, $value)
	{
		$method = 'set' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid Address property');
		}
		$this->$method($value);
	}

	public function __get($name)
	{
		$method = 'get' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid address property');
		}
		return $this->$method();
	}

	public function setOptions(array $options)
	{
		$methods = get_class_methods($this);
		foreach ($options as $key => $value) {
			$method = 'set' . ucfirst($key);
			if (in_array($method, $methods)) {
				$this->$method($value);
			}
		}
		return $this;
	}
	/**
	 * @return the $_locTreeId
	 */
	public function getLocTreeId() {
		return $this->_locTreeId;
	}


	/**
	 * @return the $_address
	 */
	public function getAddress() {
		return $this->_address;
	}

	/**
	 * @return the $_code
	 */
	public function getCode() {
		return $this->_code;
	}

	/**
	 * @param field_type $_locTreeId
	 */
	public function setLocTreeId($_locTreeId) {
		$this->_locTreeId = $_locTreeId;
	}


	/**
	 * @param field_type $_address
	 */
	public function setAddress($_address) {
		$this->_address = $_address;
	}

	/**
	 * @param field_type $_code
	 */
	public function setCode($_code) {
		$this->_code = $_code;
	}
	
	/**
	 * @return the $_parentId
	 */
	public function getParentId() {
		return $this->_parentId;
	}

	/**
	 * @param field_type $_parentId
	 */
	public function setParentId($_parentId) {
		$this->_parentId = $_parentId;
	}

	/**
	 * 
	 * @return Application_Model_LocTree|NULL return parent of the loc_tree if the parent_id != 0 else return null
	 */
	public function getParent() {
		$locTreeMapper = new Application_Model_LocTreeMapper();
		if($this->_parentId != 0){
			$parent = $locTreeMapper->find($this->_parentId, new Application_Model_LocTree());
			return $parent;
		}else {
			return null;
		}
			
	}

}